﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;

namespace WickedFlame.VecDraw.Base.Controls
{
	[TemplatePart(Name="PART_PinClose",Type=typeof(Button)), TemplatePart(Name="PART_Header",Type=typeof(FrameworkElement))]
    public class VecDrawDialog : Window
    {
        FrameworkElement _dragEnabledArea;
		Button _closeButton;

        static object syncRoot = new object();

        static VecDrawDialog()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(VecDrawDialog), new FrameworkPropertyMetadata(typeof(VecDrawDialog)));
        }

        public override void OnApplyTemplate()
        {
			if (_dragEnabledArea != null)
			{
				_dragEnabledArea.MouseLeftButtonDown -= new MouseButtonEventHandler(OnDragMouseDown);
				_dragEnabledArea.MouseMove -= new MouseEventHandler(OnDragMouseMove);

				this.LocationChanged -= new EventHandler(OnWindowMoved);
				_dragEnabledArea = null;
			}

			if (_closeButton != null)
			{
				_closeButton.Click -= new RoutedEventHandler(CloseButton_Click);
				_closeButton = null;
			}

            base.OnApplyTemplate();

            _dragEnabledArea = GetTemplateChild("PART_Header") as FrameworkElement;

            if (_dragEnabledArea != null)
            {
                _dragEnabledArea.MouseLeftButtonDown += new MouseButtonEventHandler(OnDragMouseDown);
                _dragEnabledArea.MouseMove += new MouseEventHandler(OnDragMouseMove);

                this.LocationChanged += new EventHandler(OnWindowMoved);
            }

			_closeButton = GetTemplateChild("PART_PinClose") as Button;
			if(_closeButton!= null)
				_closeButton.Click += new RoutedEventHandler(CloseButton_Click);
        }

		void CloseButton_Click(object sender, RoutedEventArgs e)
		{
			this.Close();
		}

		//protected override void OnInitialized(EventArgs e)
		//{
		//    base.OnInitialized(e);

		//    //this.CommandBindings.Add(
		//    //    new CommandBinding(CollapseCommand, this.OnExecuteCommand, this.OnCanExecuteCommand));
		//    //this.CommandBindings.Add(
		//    //    new CommandBinding(HideCommand, this.OnExecuteCommand, this.OnCanExecuteCommand));
		//    this.CommandBindings.Add(
		//        new CommandBinding(CloseCommand, this.OnExecuteCommand, this.OnCanExecuteCommand));
		//}

		//private static RoutedUICommand closeCommand = null;
		//public static RoutedUICommand CloseCommand
		//{
		//    get
		//    {
		//        lock (syncRoot)
		//        {
		//            if (null == closeCommand)
		//            {
		//                closeCommand = new RoutedUICommand("C_lose", "Close", typeof(VecDrawDialog));
		//            }
		//        }
		//        return closeCommand;
		//    }
		//}

		//internal virtual void OnExecuteCommand(object sender, ExecutedRoutedEventArgs e)
		//{
		//    if (!e.Handled && e.Command == CloseCommand)
		//    {
		//        e.Handled = true;
		//        this.Close();
		//    }
		//}

		//protected virtual void OnCanExecuteCommand(object sender, CanExecuteRoutedEventArgs e)
		//{
		//    //if (State == DockableContentState.AutoHide)
		//    //{
		//    //    if (e.Command == DockingPanel.ToggleAutoHideCommand ||
		//    //        e.Command == DockingPanel.CloseCommand ||
		//    //        e.Command == DockableContent.HideCommand)
		//    //        e.CanExecute = true;
		//    //    else
		//    //        e.CanExecute = false;
		//    //}
		//    //else
		//    e.CanExecute = true;
		//}

        #region Drag & Drop Management

        bool isMouseDown = false;

        protected bool IsMouseDown
        {
            get
            {
                return isMouseDown;
            }
        }

        void OnDragMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (!e.Handled)
            {
                isMouseDown = true;

                //Drag window
                this.DragMove();
            }
        }

        void OnDragMouseMove(object sender, MouseEventArgs e)
        {
            //mouse move event and isMouseDown can only occure just after the drag of the window
            if (isMouseDown)
            {
                Point p = Mouse.GetPosition((IInputElement)this.Owner);
                isMouseDown = false;
            }
        }

        void OnWindowMoved(object sender, EventArgs e)
        {
            if (isMouseDown)
            {
            }
        }

        #endregion
    }
}
